<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="100px">
        <el-col>
          <el-form-item label="生产日期">
            <el-date-picker
              v-model="searchParam.planDateSearch"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD"
              @change="getplanDate"
            />
          </el-form-item>
          <el-form-item label="产品名称">
            <el-select
              @keyup.enter="search"
              v-model="searchParam.materialId"
              placeholder="请选择"
              clearable
              filterable
            >
              <el-option
                v-for="item in materialList"
                :key="item.id"
                :label="item.materialName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="成品包装号">
            <el-input
              v-model="searchParam.materialBarcode"
              placeholder="请输入"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item label="生产日计划号">
            <el-input
              v-model="searchParam.planNum"
              placeholder="请输入"
              clearable
              @keyup.enter="search"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search"
              ><i-ep-search />搜索</el-button
            >
            <el-button @click="reset"><i-ep-refresh />重置</el-button>
          </el-form-item>
        </el-col>
        <el-col>
          <el-form-item>
            <el-button
              type="primary"
              @click="
                useDownload(
                  '/mes/mesproducingstockrecord/exportPlanForm',
                  '成品包装信息',
                  searchParam
                )
              "
            >
              <template #icon><i-ep-download /></template>导出</el-button
            >
          </el-form-item>
        </el-col>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column type="selection" width="55" align="center" />
        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          label="包装条码"
          prop="materialBarcode"
          min-width="80"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="产品图号"
          prop="figureNumber"
          min-width="80"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="产品名称"
          prop="materialName"
          min-width="80"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="生产日计划号"
          prop="planNum"
          min-width="80"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="数量"
          prop="planQuantity"
          min-width="40"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="发货单号"
          prop="materialQuantity"
          min-width="100"
          show-overflow-tooltip
          align="left"
        />
        <el-table-column
          label="库存状态"
          prop="quantityUnit"
          min-width="40"
          show-overflow-tooltip
          align="left"
        />

        <el-table-column
          label="线边库"
          prop="produceStorageName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="是否合格"
          prop="isValid"
          min-width="40"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.isFeedMaterial === 1">是</el-tag>
            <el-tag v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作用户"
          prop="lastUpdatedUserId"
          min-width="40"
          show-overflow-tooltip
          align="left"
        />

        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="80"
          show-overflow-tooltip
          align="center"
        /> -->
      </el-table>
      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDownload } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESPRODUCESTORAGE",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesproducingstockrecord/queryByPagePlanForm";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  materialCode: "",
  materialName: "",
  materialType: "",
  plantId: "",
  figureNumber: "",
};
// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(); // 无需二次数据处理&不传赞数也行
const materialList: any = ref([]);
async function materialListFunction() {
  const plantList = await request({
    url: "mes/mdmmaterial/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  materialList.value = plantList.data;
}
// const dataCallBack = ref(undefined);
// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError
);

onMounted(() => {
  getTableList();
  materialListFunction();
});

function getplanDate(data: any) {
  if (data) {
    searchParam.value.startDate = data[0];
    searchParam.value.endDate = data[1];
  } else {
    searchParam.value.startDate = "";
    searchParam.value.endDate = "";
  }
}

const tableColumns = reactive([
  {
    prop: "materialBarcode",
    label: "包装条码",
    minWidth: 80,
    isShow: true,
    align: "left",
  },
  {
    prop: "figureNumber",
    label: "产品图号",
    minWidth: 80,
    isShow: true,
    align: "left",
  },
  {
    prop: "materialName",
    label: "产品名称",
    minWidth: 80,
    isShow: true,
    align: "left",
  },
  {
    prop: "planNum",
    label: "生产日计划号",
    minWidth: 80,
    isShow: true,
    align: "left",
  },
  {
    prop: "planQuantity",
    label: "数量",
    minWidth: 40,
    isShow: true,
    align: "right",
  },
  {
    prop: "materialQuantity",
    label: "发货单号",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "quantityUnit",
    label: "库存状态",
    minWidth: 40,
    isShow: true,
    align: "left",
  },
  {
    prop: "produceStorageName",
    label: "线边库",
    minWidth: 100,
    isShow: true,
    align: "center",
  },
  {
    prop: "isValid",
    label: "是否合格",
    minWidth: 40,
    isShow: true,
    align: "center",
    render: (scope: any) =>
      scope.row.isValid == 1 ? <el-tag>是</el-tag> : <el-tag>否</el-tag>,
  },
  {
    prop: "lastUpdatedUserId",
    label: "操作用户",
    minWidth: 40,
    isShow: true,
    align: "left",
  },
  {
    prop: "createTime",
    label: "创建时间",
    minWidth: 80,
    isShow: true,
    align: "center",
  },
]);

const searchColumns = reactive([]);
</script>
<style lang="scss" scoped></style>
